<?
require_once("db_mysql.class.php");

define("MAX_WORD_LENGTH",34);

class smart_DB_Sql extends DB_Sql{

      var $logFunction = "logEvent";


      function putRow($table,$data,$id=""){
       if(!is_array($data)) $this->halt("No data to insert");

       //getting the fields list for the table
       $fields_list=$this->getFieldList($table);
       foreach($data as $k=>$v){
        if(!array_key_exists($k,$fields_list)){
          unset($data[$k]);
          continue;
        }
       }

       //unsetting the key field

       $flds=array();
       foreach($data as $k=>$v){
        if($k==$fields_list['key']['Field']) continue;
        	if($fields_list[$k]['Type']=='blob'){ // AES encryption
        		$flds[]="$k= AES_ENCRYPT('$v','{$this->AES_key}')";
        	}else{
        		$flds[]="$k='$v'";
        	}	
       }
       $sql_set=implode(", ",$flds);
       if($id==""){
        //checking whenever this is update or insert
        if(@isset($fields_list['key'])&&(@isset($data[$fields_list['key']['Field']]))&&($data[$fields_list['key']['Field']]!="")){

         //update
         $sql="UPDATE $table SET $sql_set WHERE {$fields_list['key']['Field']}='{$data[$fields_list['key']['Field']]}'";

        }
        else{

         //insert
         $sql="INSERT INTO $table SET $sql_set";

        }
       }
       elseif(!is_array($id)&&$id!=""&&isset($fields_list['key'])){

        //update but with the key value set
        $sql="UPDATE $table SET $sql_set WHERE {$fields_list['key']['Field']}='$id'";

       }
       elseif(is_array($id)){

        //update but with full update params set
        $sql_where="";
        foreach($id as $k=>$v){
         $sql_where.=" AND $k='$v'";
        }
        $sql="UPDATE $table SET $sql_set WHERE 1=1 $sql_where";

       }
       $this->query($sql);
      }
      function getRow($key){

      }

		
	function getAssocArray($table,$valueField,$sortByKey=0){
		$fields=$this->getFieldList($table);
		$keyField=$fields['key']['Field'];

		$sql="SELECT * FROM ".$table." ORDER BY ".($sortByKey?$keyField:$valueField);
		$this->query($sql);
		$records=array();
		while ($this->next_record()) {
			$records[$this->Record[$keyField]]=$this->Record[$valueField];
		}
		
		return $records;
		
	}

      function getFieldList($table){
       $sql="show fields from $table";
       $this->query($sql);
       $r=$this->getArray();
       foreach($r as $v){
        $fl[$v['Field']]=$v;
        if($v['Extra']=="auto_increment") $fl['key']=$v;
       }
       return $fl;
      }

      function haltmsg($msg){
         $s=sprintf("mysql error: %s (%s)",$this->Errno,$this->Error);
         if($this->logFunction){
         	call_user_func($this->logFunction,$msg."\t".$s);
         }
         echo "</td></tr></table>
         <div align='center'>
         <h1>Sorry...an error occured.</h1>
         Please try reloading the page as it may have been a temporary glitch.<br>
         Thanks for helping us improve!<br></div>";
         exit;
      }

      function query($sql){

       $time_start=microtime_float();
       $time=0;

       if($this->Debug&&$this->logFunction){
       	$sql1=preg_replace("@[\r\n\t]@"," ",$sql);
         call_user_func($this->logFunction,$sql1."\t".$time);
       }
       $result = DB_Sql::query($sql);
       $time= microtime_float() - $time_start;
       if($this->Debug&&$this->logFunction){
         call_user_func($this->logFunction,$sql1."\t".$time);
       }

       return $result;

      }
      //fixing nasty TOO long words
/*
      function next_record(){
      	$result=DB_Sql::next_record();
         foreach($this->Record as $k=>$v){
		if(preg_match("@\s\w{".MAX_WORD_LENGTH.",}@",$v)){
	        	$this->Record[$k]= wordwrap($v,MAX_WORD_LENGTH," ",1);
                         echo $v;
	        }
         }
         return $result;
      }
*/

}
?>